package admin.controllers;

import helpers.IdUniqueHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import admin.data.DealData;
import admin.data.ProductData;
import admin.data.StoreData;
import admin.data.StoreItemData;
import admin.data.TransactionData;

import models.DealModel;
import models.ProductModel;
import models.StoreItemModel;
import models.StoreModel;
import models.TransactionModel;

public class ListStoreItemServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");

		String path = ((HttpServletRequest) request).getRequestURI();
		String params = path.replaceAll("/admin/store-item/list/", "");

		StoreItemData storeItemData = new StoreItemData();
		StoreItemModel storeItemModel = new StoreItemModel();
		List<StoreItemModel> listStoreItem = new ArrayList<StoreItemModel>();

		if (params.length() > 3) {
			storeItemModel = storeItemData.getItem(params);
			if (storeItemModel.getId().length() > 3) {
				storeItemModel.setOrder(ProductData.getById(storeItemModel.getProductId()).getTotalOrderQuantity());
				listStoreItem.add(storeItemModel);
			}
		} else {
			listStoreItem = storeItemData.getListItem();
		}

		request.setAttribute("listStoreItem", listStoreItem);

		try {
			request.getRequestDispatcher("/admin/pages/list_store_item.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");

		String action = (String) request.getParameter("action");

		if (action.equalsIgnoreCase("search_product")) {
			String productId = (String) request.getParameter("productId");
			String type = (String) request.getParameter("type");
			List<StoreItemModel> listStoreItem = new ArrayList<StoreItemModel>();
			if(productId.length()>3)
			{
				StoreItemData storeItemData = new StoreItemData();
				StoreItemModel storeItemModel = storeItemData.getItem(productId);
				if(storeItemModel.getId().length()>3)
				{
					storeItemModel.setOrder(ProductData.getById(productId).getTotalOrderQuantity());
					listStoreItem.add(storeItemModel);
				}
			}
			else if(type.length()>0)
			{
				StoreItemData storeItemData = new StoreItemData();
				listStoreItem = storeItemData.getListItemByType(Integer.parseInt(type));
			}
			request.setAttribute("listStoreItem", listStoreItem);
		}
		try {
			request.getRequestDispatcher("/admin/pages/list_store_item.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}